package com.noodles.mall.controller;

import com.noodles.mall.entity.Role;
import com.noodles.mall.entity.User;
import com.noodles.mall.service.RoleService;
import com.noodles.mall.vo.PageVo;
import com.noodles.mall.web.JsonResult;
import com.noodles.mall.web.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 角色 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /** 增加 **/
    @RequestMapping("/insert")
    public JsonResult insert(@RequestBody Role role) {
        System.out.println("RoleController: insert");
        System.out.println("role: "+role);
        if(roleService.STRING_ROLE_MAP.containsKey(role.getName()))
            return JsonResult.error(State.ERR_EXIST);
        return roleService.insert(role)
                ? JsonResult.success(role.getId()) : JsonResult.error(State.ERR_SQL);
    }

    /** 删除 **/
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody List<Integer> ids) {
        System.out.println("RoleController: delete");
        System.out.println("ids: "+ids);
        //没写
        return JsonResult.success();
    }

    /** 修改 **/
    @RequestMapping("/update")
    public JsonResult update(@RequestBody Role role) {
        System.out.println("RoleController: update");
        System.out.println("role: "+role);
        return roleService.updateById(role)
                ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 查询列表:  **/
    @RequestMapping("/selectList")
    public JsonResult selectList() {
        System.out.println("RoleController: selectList");
        List<Role> roleList = roleService.selectList();
        return JsonResult.success(roleList);
    }

    /** 分页查询: 无 **/
    @RequestMapping("/selectPage/{current}/{size}")
    public JsonResult selectPage(@PathVariable(value = "current") Long current,
                                 @PathVariable(value = "size") Long size) {
        System.out.println("RoleController: selectPage");
        PageVo<User> pageVo = roleService.selectPage(current,size);
        return JsonResult.success(pageVo);
    }

    /** 分页查询: word **/
    @RequestMapping("/selectPageByWord/{current}/{size}/{word}")
    public JsonResult selectPageByWord(@PathVariable(value = "current") Long current,
                                 @PathVariable(value = "size") Long size,
                                 @PathVariable(value = "word") String word) {
        System.out.println("RoleController: selectPage");
        System.out.println("word: "+word);
        PageVo<User> pageVo = roleService.selectPage(current,size,word);
        return JsonResult.success(pageVo);
    }
}
